443C - Borya and Hanabi - CodeForces Solution


bitmasks brute force constructive algorithms implementation *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int mat[5][5];
int mp[255];
bool check(int mask1, int mask2){ 
    bool marked_num[5]{},marked_col[5]{};
    bool flag=0;
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            if(!mat[i][j])continue;
            int r = -1; 
            if(!((1<<i) & mask1)){
                if(marked_num[j])return 0;
                marked_num[j]=i+1;
                ++r;
            }
            if(!((1<<j) & mask2)){
                if(marked_col[i])return 0;
                marked_col[i]=j+1;
                ++r;
            }
            if(r==1){
                if(flag)return 0;
                flag=1;
            }
        }
    } 
    for(int i = 0; i < 5; i++){
        if(marked_num[i] && marked_col[marked_num[i]-1] != 0 && marked_col[marked_num[i] - 1] != marked_num[i])
            return 0;
        if(marked_col[i] && marked_num[marked_col[i]-1] != 0 && marked_num[marked_col[i]-1] != marked_col[i])
            return 0;
    }
    return 1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    mp['R']=0;
    mp['G']=1;
    mp['B']=2;
    mp['Y']=3;
    mp['W']=4;
    int n;
    cin>>n; 
    for(int i = 0; i < n; i++){
        string s; 
        cin>>s;
        mat[mp[s[0]]][s[1]-'1']=1;
    }
    int ans = 10;
    for(int mask1 = 0; mask1 < (1 << 5); mask1++){
        for(int mask2 = 0; mask2 < (1 << 5); mask2++){
            if(check(mask1,mask2)){
                ans=min(ans,__builtin_popcount(mask1)+__builtin_popcount(mask2));
            }
        }
    }
    cout<<ans;
    return 0;
}


Comments

Submit
0 Comments
More Questions

620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll